package com.hsy.bibc.modules.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.R;
import com.hsy.bibc.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/4/8.
 */
public class MyCommonEditActivity extends CommonBaseActivityWithSimpleActionBar {
    private Intent intent;
    private TextView text, finish;
    private EditText edit_text;
    private String typeTitle;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                CommonUtils.hideProgressDialog();
                finish();
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            CommonUtils.hideProgressDialog();
        }
    };
    private String nickName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        intent = getIntent();
        typeTitle = intent.getStringExtra(MyInformationActivity.TITLE);

        setTitle(typeTitle);
        setTitleTextColor(R.color.tab_text_color_normal);
        setContentView(R.layout.activity_change_person_edit);

        Intent intent = getIntent();
        nickName = intent.getStringExtra("name");

        text = F(R.id.text);
        edit_text = F(R.id.edit_text);
        finish = F(R.id.finish);

        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeTitle.equals("修改昵称")) {

                    if (checkName(edit_text.getText().toString())) {
                        postHttp();
                    }

                } else if (typeTitle.equals("修改联系电话")) {

                }


            }
        });
        showUI();


        edit_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isEmpty(edit_text.getText().toString())) {
                    finish.setTextColor(getResources().getColor(R.color.tab_text_color_normal));
                    finish.setBackgroundResource(R.drawable.frame_sign_b9b9b9);
                } else {
                    finish.setTextColor(getResources().getColor(R.color.color_action_bar_bg));
                    finish.setBackgroundResource(R.drawable.frame_sign_ec1919);
                }
            }
        });
    }

    private boolean checkName(String s) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
        if (m.find()) {
            Toast.makeText(MyCommonEditActivity.this, getString(R.string.msg_limit_name), Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }


    private void postHttp() {
        if (!isEmpty(edit_text.getText().toString())) {
            CommonUtils.showProgressDialog(MyCommonEditActivity.this);
            Map<String, String> data = new HashMap<>();
            data.put("msg", edit_text.getText().toString());
            HttpUtil.httpPostWithParams(this, 0, Consts.API_GET_MY_CHANGE_NAME, data, callBack);
        } else {
            showToast(getString(R.string.forget_nick_name));
        }
    }

    private void showUI() {
        if (typeTitle.equals("修改昵称")) {
            text.setText("昵称由中文.英文.数字.-和_组成");
            edit_text.setHint("昵称");
            if (null != nickName) {
                edit_text.setText(nickName);
                finish.setTextColor(getResources().getColor(R.color.color_action_bar_bg));
                finish.setBackgroundResource(R.drawable.frame_sign_ec1919);
            } else {
                finish.setTextColor(getResources().getColor(R.color.tab_text_color_normal));
                finish.setBackgroundResource(R.drawable.frame_sign_b9b9b9);
            }


        } else if (typeTitle.equals("修改联系电话")) {
            text.setText("请填写您的真实联系电话，这样他人会更方便的找到您。");
            edit_text.setHint("请输入联系电话");
            if (null != nickName) {
                edit_text.setText(nickName);
                finish.setTextColor(getResources().getColor(R.color.color_action_bar_bg));
                finish.setBackgroundResource(R.drawable.frame_sign_ec1919);
            } else {
                finish.setTextColor(getResources().getColor(R.color.tab_text_color_normal));
                finish.setBackgroundResource(R.drawable.frame_sign_b9b9b9);
            }

        } else if (typeTitle.equals("修改姓名")) {
            text.setText("*请务必填写真实姓名");
            edit_text.setHint("姓名");

        }
    }
}
